package lowijs.XfireClient;

import java.security.*;

public class Util {
	/**
	 * Hashes a plain text string with the SHA1 algorithm.
	 * @param plain a plain text string.
	 * @return the hashed string.
	 */
	public static String toSHA1(String plain) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(plain.getBytes());
			byte[] hash = md.digest();
			StringBuffer hexstring = new StringBuffer();
			for (int i = 0; i < hash.length; i++) {
				String b = Integer.toHexString(0xFF & hash[i]);
				if (b.length() == 1)
					b = "0" + b;
				hexstring.append(b);
			}
			return hexstring.toString().toLowerCase();
		} catch (NoSuchAlgorithmException nsae) {
			return null;
		}
	}
}
